﻿@using System.Data
@{
    ViewBag.Title = "Index";
    Layout = "~/Areas/Admin/Views/Shared/AdminLayout.cshtml";
    DataTable table = ViewData["sort"] as DataTable;
}

<div class="searchpanel">
    <div class="seatchtool">
        <div class="operationtool">
            <a id="edit" onclick="edit(0)">修改</a>
            <a id="add">增加</a>
            <a id="del">删除</a>
           
        </div>
        <div class="operationsearch">
            <label>会员名称：</label><input type="text" id="name" value="" style="height: 20px; float: left; " />
            <label>会员电话：</label><input type="text" id="tell" value="" style="height: 20px; float: left; " />
            <a id="search">查询</a>
        </div>
    </div>
</div>
<table class="datatable">
    <tr>
        <th width="30">选择<input type="checkbox" id="checkall" /></th>
        <th width="200">姓名</th>
        <th width="200">头像</th>
        <th width="200">电话</th>
        <th width="200">性别</th>
        <th width="100">余额</th>
        <th width="200">操作</th>
        <th></th>
    </tr>
    <tbody id="list">
        
    </tbody>


</table>
<div id="Pagination" class="pagination" style="">
</div>
<link href="/Scripts/paging/pagination.css" rel="stylesheet" />
<script src="/Scripts/paging/jquery.pagination.js"></script>


<script>

    $(function () {
        getlist(0);
        //选择全部
        $("#checkall").click(function () {
            if ($("#checkall").attr("checked")) {
                $("#list input[type=checkbox]").attr("checked", "true");
            } else {
                $("#list input[type=checkbox]").removeAttr("checked");
            }
        });
    });
    //添加
    $("#add").click(function () {
        location.href = "@Url.Action("Window",new {id=0})";
    });
    function del(id) {
        if (confirm("确定删除数据吗？")) {

            $.post("@Url.Action("Del")", { id: id }, function (row) {
                if (row > 0) {
                    location.reload();
                } else {
                    alert("删除失败！");
                }
            });
    }
    }
    //修改
    function edit(id) {
        $("#list input[type=checkbox]").each(function() {
            if ($(this).attr("checked")) {
                id = $(this).val();
                return false;
            }
        });
        if (id == 0) {
            alert("请选择要修改的数据！");
        } else {
            location.href = "@Url.Action("Window")" + "/" + id;
        }
    }
    
    //查询
    $("#search").click(function () {
        getlist(0);
    });

    //删除
    $("#del").click(function () {
        if (confirm("确定删除数据吗？")) {
            var id = "";
            $("#list input[type=checkbox]").each(function () {
                if ($(this).attr("checked")) {
                    id += $(this).val() + ",";
                }
            });
            if (id == "") {
                alert("请选择要修改的数据！");
                return false;
            }
            id = id.substring(0, id.length - 1);

            $.post("@Url.Action("Del")", { ids: id }, function (row) {
                if (row > 0) {
                    getlist(0);
                } else {
                    alert("删除失败！");
                }
            });
        }

    });
   
    var pageCount = 10;

    function getlist(pageIndex) {
        var name = $("#name").val();
        var tell = $("#tell").val();
        $.post("@Url.Action("GetList")", { pageIndex: pageIndex * pageCount, pageCount: pageCount,name:name,tell:tell}, function (data) {
            var obj = JSON.parse(data);
            var count = obj.count[0].count;
            var str = "";
            for (var i = 0; i < obj.data.length; i++) {
                var item = obj.data[i];
                str += "<tr><td><input type=\"checkbox\"  value=" + item.Id + " /></td>" +
                    "<td>" + item.Name + "</td>" +
                    "<td><img src='" +item.Img + "' width='100'/></td>" +
                    "<td>" + item.Tell + "</td>" +
                       "<td>" + (item.Sex==1?"男":"女") + "</td>" +
                      "<td>" + item.Money + "</td>" +
                    "<td>" +
                    "<a class=\"ope_bt\" onclick=\"edit(" + item.Id + ")\">修改</a>" +
                    
                    "</td><td></td>" +
                    "</tr>";
            }

            $("#list").html(str);

            $("#Pagination").pagination(count, {
                callback: PageCallback,
                prev_text: '上一页', //上一页按钮里text
                next_text: '下一页', //下一页按钮里text
                items_per_page: pageCount, //显示条数
                num_display_entries: 5, //连续分页主体部分分页条目数
                current_page: pageIndex, //当前页索引
                num_edge_entries: 1 //两侧首尾分页条目数
            });

        });
    }
    //分页回调函数
    function PageCallback(index) {
        getlist(index);
    }

</script>



